import { defineStore } from 'pinia';
import { ref } from 'vue';

export const useFriendStore = defineStore('friend', () => {
  const friends = ref([]);
  const selectedFriend = ref(null);

  const selectFriend = (friend) => {
    selectedFriend.value = friend;
  };

  const changeFriends=(newVal) =>{
    friends.value = newVal;
  }
  const addFriend = (newFriend)=>{
    const friendData = {
      id: Date.now().toString(),
      name: newFriend.senderId, 
      avatar: 'https://picsum.photos/200/200',
      friendId: newFriend.senderId,
      remark: newFriend.senderId,
    };
    
    // 使用展开运算符创建新数组，确保响应式更新
    friends.value = [...friends.value, friendData];
    console.log("响应式更新friends",friends.value);
  }

  return {
    selectedFriend,
    selectFriend,
    friends,
    changeFriends,
    addFriend
  };
});
